//开始上传代码
const ci = require('miniprogram-ci');
const initStayUploadInfo = require('./stayUploadInfo');
async function uploadCode (path){
    if (!path) {
      console.log('key文件或带上传文件没有找到');
      return
    }
    if (Object.prototype.toString.call(path) !== '[object Object]') {
       console.log('路径传输格式不正确');
       return;
    }
    if (!('keyFileListPath' in path && 'buildFileListPath' in path) && path.keyFileListPath == '' || path.buildFileListPath == '') {
      console.log('key文件或上传文件 参数不对');
      return
    }
    let stayUploadInfo = await initStayUploadInfo(path);
    for (let i = 0; i < stayUploadInfo.length; i++) {
        return await uploadWeiXinCode(projectInfo(stayUploadInfo[i]),stayUploadInfo[i].projectPath); 
    }
};
//创建待上传项目的项目信息
const projectInfo = (prject) =>{
   return new ci.Project(prject);
}
const uploadWeiXinCode = async function (project,projectPath){
     const uploadResult = await ci.upload({
       project,
       version: versionNumber(),
       desc: '上传代码',
       setting: {
        "urlCheck" : true,
        "es6" : false,
        "postcss" : true,
        "minified" : true
       },
       threads:2,
       useCOS:true,
       onProgressUpdate: console.log('正在上传:',projectPath),
     })
     console.log(uploadResult,'上传结果')
}


versionNumber = ()=>{
    const newDate = new Date;
    var o = {
      "M+": newDate.getMonth() + 1, // 月份
      "d+": newDate.getDate(), // 日
      "h+": newDate.getHours(), // 小时
      "m+": newDate.getMinutes(), // 分
      "s+": newDate.getSeconds(), // 秒
      "q+": Math.floor((newDate.getMonth() + 3) / 3), // 季度
      "S": newDate.getMilliseconds() // 毫秒
    };
    return `${String(o['M+']).padStart(2, '0')}.${String(o['d+']).padStart(2, '0')}.${String(o['h+']).padStart(2, '0')}.${String(o['m+']).padStart(2, '0')}`
}

module.exports = uploadCode;